import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const _ = require('lodash')
export default new Vuex.Store({
  state: {
    heros: [],
    items: [],
    category: [],
    inscs: [],
    skills: []
  },
  mutations: {
    // 操作heros
    Hero (state, step) {
      state.heros = step
      window.sessionStorage.setItem('Hero', JSON.stringify(state.heros))
    },
    UpdataHero (state, step) {
      state.heros.forEach((HeroCate, index, arr) => {
        if (HeroCate._id === step._id) {
          arr[index].children = _.cloneDeep(step.children)
        }
      })
      window.sessionStorage.setItem('Hero', JSON.stringify(state.heros))
    },
    // 操作items
    Item (state, step) {
      state.items = step
      window.sessionStorage.setItem('Item', JSON.stringify(state.items))
    },
    UpdataItem (state, step) {
      state.items.forEach((ItemCate, index, arr) => {
        if (ItemCate._id === step._id) {
          arr[index].children = _.cloneDeep(step.children)
        }
      })
      window.sessionStorage.setItem('Item', JSON.stringify(state.items))
    },
    // 操作category
    Cate (state, step) {
      state.category = step
      window.sessionStorage.setItem('Cate', JSON.stringify(state.category))
    },
    addCate (state, step) {
      state.category.push(step)
      window.sessionStorage.setItem('Cate', JSON.stringify(state.category))
    },
    UpdataCate (state, step) {
      state.category.forEach((cate, index, arr) => {
        if (cate._id === step._id) {
          arr[index] = step
        }
      })
      window.sessionStorage.setItem('Cate', JSON.stringify(state.category))
    },
    deleteCate (state, step) {
      state.category = state.category.filter(item => item._id !== step)
      window.sessionStorage.setItem('Cate', JSON.stringify(state.category))
    },
    // 操作inscs
    Insc (state, step) {
      state.inscs = step
      window.sessionStorage.setItem('Insc', JSON.stringify(state.inscs))
    },
    UpdataInsc (state, step) {
      state.inscs.forEach((insc, index, arr) => {
        if (insc._id === step._id) {
          arr[index].children = _.cloneDeep(step.children)
        }
      })
      window.sessionStorage.setItem('Insc', JSON.stringify(state.inscs))
    },
    // 操作skills
    Skill (state, step) {
      state.skills = step
      window.sessionStorage.setItem('Skill', JSON.stringify(state.skills))
    },
    addSkill (state, step) {
      state.skills.push(step)
      window.sessionStorage.setItem('Skill', JSON.stringify(state.skills))
    },
    UpdataSkill (state, step) {
      state.skills.forEach((skill, index, arr) => {
        if (skill._id === step.id) {
          arr[index] = _.cloneDeep(step.model)
        }
      })
      window.sessionStorage.setItem('Skill', JSON.stringify(state.skills))
    },
    deleteSkill (state, step) {
      state.skills = state.skills.filter(item => item._id !== step)
      window.sessionStorage.setItem('Skill', JSON.stringify(state.skills))
    }

  },
  actions: {},
  modules: {}
})
